package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import Models.POSProductServingType;
import Models.POSProducts;

public class POSProductsPresenter extends AbstractPresenter<POSProducts> {
	final String SINGLEITEM = "posProducts";
	final String MULTIITEM = "pOSProductss";
	
	public ArrayList<POSProducts> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public POSProducts tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public POSProducts createItemFromJSONObject(JSONObject jsonProduct){
		POSProducts product = new POSProducts();
		try{
			product.setId(jsonProduct.getInt("id"));
			product.setDescription(jsonProduct.getString("productDescription"));
			product.setOnTap(jsonProduct.getBoolean("productOnTap"));
			//Build Beer
			if(!jsonProduct.isNull("productBeer")){
				POSBeerViewPresenter beerPresenter = new POSBeerViewPresenter();
				JSONObject jsonBeer = jsonProduct.getJSONObject("productBeer");
				product.setBeer(beerPresenter.createItemFromJSONObject(jsonBeer));
			} else {
				product.setBeer(null);
			}
			//Set POSProductServingTypeCollection.
			ArrayList<POSProductServingType> psts = new ArrayList<POSProductServingType>();
			if(!jsonProduct.isNull("servingTypeCollection")){
				Object item = jsonProduct.get("servingTypeCollection");
				POSProductServingTypePresenter pstPresenter = new POSProductServingTypePresenter();
				if(item instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)item;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonPST = jPSTArray.getJSONObject(i);
						psts.add(pstPresenter.createItemFromJSONObject(jsonPST));
					}
				} else {
					JSONObject jsonPST = (JSONObject)item;
					psts.add(pstPresenter.createItemFromJSONObject(jsonPST));
				}
				product.setProductServingTypes(psts);
			}
			return product;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(POSProducts item){
		try{
			JSONObject json = new JSONObject();
			json.put("id", item.getId());
			json.put("productDescription", item.getDescription());
			json.put("productOnTap", item.isOnTap());
			if(item.getBeer()!=null){
				POSBeerViewPresenter beerPresenter = new POSBeerViewPresenter();
				json.put("productBeer", beerPresenter.createJSON(item.getBeer()));
			} else {
				json.put("productBeer", JSONObject.NULL);
			}
			if(item.getProductServingTypes()!=null){
				POSProductServingTypePresenter pstPresenter = new POSProductServingTypePresenter();
				if(item.getProductServingTypes().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getProductServingTypes().size(); i++){
						jsonArray.put(pstPresenter.createJSON(item.getProductServingTypes().get(i)));
					}
					json.put("servingTypeCollection", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSProducts getEmptyEntity(){
		POSProducts entity = new POSProducts();
		return entity;
	}
}
